﻿var tdBrand=0,tdCar=0,tdStyle=0,tdType=0,tdYear=0,k=0,tdFormat=0,did=0;
var act;
var gCurPage = 1;
var gTotalPage = 0;
var gQueryUrl = "";
var gId;
function getQueryString(name) {
   var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
   var r = window.location.search.substr(1).match(reg);
   if (r!=null) return (r[2]); return null;
}
$(function(){
		k = getQueryString("k");
		if(k==null)
		{
			k = 0;
		}
		act = $("#act").val();
		//query();
		var selop = "<td><a data='tdYear' onclick='tdYear=0;query(this);'>ALL</a></td><td><ul>";
	    for(i=2015;i<=new Date().getFullYear();i++)
		{
			selop = selop+"<li><a data='tdYear' onclick='tdYear="+i+";query(this);'>"+i+"</a></li>";	
		}
		$("#tdYear").html("<table cellspacing='0' cellpadding='0' id='tblsel_tdYear'>"+selop+"</li></ul></td></table>");
		
		$("#tdCar").html("<a onclick='tdCar=0;query(obj);'>ALL</a>");
	    initSelect("tdBrand",$("#tdBrand").attr("did"));
		initSelect("tdType",$("#tdType").attr("did"));
		initSelect("tdFormat",$("#tdFormat").attr("did"));
		$("#tdStyle").html("<a onclick='tdStyle=0;query(obj);'>ALL</a>");
		//initSelect("tdStyle",$("#tdStyle").attr("did"));
		$("#ulRef").find("li").each(function(){
		    var jq = $(this);
		    jq.click(function(){
		         var t = jq.find("div").html();
		         setHeaderPos(t);
		         getPage("index.php?act=ajax&op=getLstFile&k=subject"+t);
		    });
		});
 //getPage("../index.php?act=ajax&op=getLstFile&b="+tdBrand+"&c="+tdCar+"&t="+tdType+"&s="+tdStyle+"&y="+tdYear+"&k="+k);
	});
function openDetail(id,isGroupTop,title)
{
	//window.location.href="detail.php?id="+id;
	if(isGroupTop=="0")
	{
	gId = id;
    $("#trListData").hide();
	$("#trDetail").show();
	getContent("../index.php?act=ajax&op=getFileInfo&src=m&id="+id);
	}
	else
	{
		$("#groupTitle").html(title);
		getPage("index.php?act=ajax&op=getLstFile&b="+tdBrand+"&c="+tdCar+"&t="+tdType+"&s="+tdStyle+"&y="+tdYear+"&k="+k+"&f="+tdFormat+"&did="+did+"&gid="+id		);
	}
}
function getPage(url)
{
    	$("#trDetail").hide();
	$("#trListData").show();

		$("#loading").html("loading.....");
		$("#loading").show();
		$("#ulList").html("");
		$.get("../"+url+"&src=m",function(data){
		var j = eval("("+data+")");
		if(j.st=="ok")
		{
			if(j.data.total>0)
			{
				$("#loading").hide();
				var ul = "";
				for(i=0;i<j.data.data.length;i++)
				{
 					var xp = j.data.data[i].pic.replace("s.","sm.");
                    var groupImg = "";
					if(j.data.data[i].isGroupTop!="0")
					{
						groupImg='<img src="../images/group.png" style="width:20px;height:20px;right: 3px;position: absolute;bottom: 2px;">';
					}
					ul = ul+'<li><div onclick="openDetail('+j.data.data[i].id+','+j.data.data[i].isGroupTop+',\''+j.data.data[i].mark+'\');" style="background-size:cover;  width:95px;height:95px;background-image:url(../'+xp+'); background-position:center;position:relative;">'+groupImg+'</div></li>';
				}
				$("#ulList").append(ul);
			}
			else
			{
				$("#loading").html("找不到资源");
			}
			gQueryUrl = j.u;
			gTotalPage = j.tp;
			$("#tdTotalPage").html(j.tp);
			$("#total").html(j.data.total);
			$("#divPageNav").html(j.data.pageNav);
	 
			$("#totalNum").html("共有&nbsp;&nbsp;&nbsp;"+j.data.total+"&nbsp;&nbsp;&nbsp;张图片");
		}
		});
}
 
function initSelect(obj,id)
{
	//alert(id);
	$.get("../index.php?act=upload&op=ajaxType&id="+id,function(data){
		//alert(data);
		var json = eval("("+data+")");	
		if(json.length>0)
		{
			var selop = "<td valign='middle'><a data='"+obj+"' onclick='"+obj+"=0;query(this);'>ALL</a></td><td><ul>";
			
			for(i=0;i<json.length;i++)
			{
				if(id==1 || id==2 || id==3|| id==4||id==5)
				{
					selop = selop+"<li><a id='sel_"+id+"' data='"+obj+"' onclick='"+obj+"="+json[i].id+";query(this);'><img src='../images/ico/"+json[i].id+".png'/></a></li>";
				}
				else
				{
					selop = selop+"<li><a data='"+obj+"' onclick='"+obj+"="+json[i].id+";query(this);'>"+json[i].name+"</a></li>";
				}
			}
			selop = selop+"</ul></td>";
			$("#"+obj).html("<table cellspacing='0' cellpadding='0' id='tblsel_"+obj+"'>"+selop+"</table>");
			if(obj=="tdStyle")
			{
				$("#trStyle").show();
			}
		}
		else
		{
			var selop = "<a data='"+obj+"' onclick='"+obj+"=0;query(this);'>ALL</a>";
			$("#"+obj).html(selop);
			if(obj=="tdStyle")
			{
				tdStyle = 0;
				$("#trStyle").hide();
			}
		}
		if($("#"+obj).attr("sel")!="")
		{
			$("#"+obj).val($("#"+obj).attr("sel"));
			$("#"+obj).change();
		}
		if(obj=="tdBrand")
		{
			tdBrand=2;
			query(document.getElementById("sel_1"));
		}
	});
}
function clearQueryParam()
{
	tdBrand=0;
	tdCar=0;
	tdStyle=0;
	tdType=0;
	tdYear=0;
	tdFormat=0;
	did = 0;
}
function query(o)
{
   var jq = $(o);
	var fromGroup = jq.attr("data");
	if(o!=null)
	{
		 
		if(fromGroup=="tdBrand" || fromGroup=="tdCar")
		{
			$("#tblsel_"+fromGroup).find("img").each(function(){
				$(this).css("border","none");
			});
		}
		else
		{
			$("#tblsel_"+fromGroup).find("a").each(function(){
				$(this).css("color","#000000");
			});
		}
		var img = $(o).find("img")[0];
		if(typeof(img) =="undefined")
		{
			o.style.color="#ffc019";
		}
		else
		{
			if(fromGroup=="tdCar")
			{
				 
				$(o).find("img").css("border-bottom","1px #ffc019 solid");
			}
		}
	}
	if(tdBrand!=0 && fromGroup=="tdBrand")
	{
		initSelect("tdCar",tdBrand);
	}
	if(tdType!=0  && fromGroup=="tdType")
	{
		initSelect("tdStyle",tdType);
		tdStyle = 0;
	}
	getPage("index.php?act=ajax&op=getLstFile&b="+tdBrand+"&c="+tdCar+"&t="+tdType+"&s="+tdStyle+"&y="+tdYear+"&k="+k+"&f="+tdFormat+"&did="+did);
	 
}
function toPage(idx)
{
 
	if(idx==-1)
	{
		gCurPage = gCurPage -1;
		if(gCurPage<1)
		{	
			gCurPage = 1;
			alert("已到第一页");
			return;
		}
	}
	else if(idx==1)
	{
		gCurPage = gCurPage +1;
		if(gCurPage>gTotalPage)
		{
			alert("已到末尾页");
			gCurPage = gCurPage-1;
			return;
		}
	}
	$("#txtPageNum").val(gCurPage);
	getPage(gQueryUrl+"&page="+gCurPage);
	 
}
function showSet()
{
	$("#querySet").toggle();
}

function getContent(url)
{
		$.get(url,function(data){
		var j = eval("("+data+")");
		if(j.st=="ok")
		{
			//$("#showImg")[0].src="../"+j.data.path;;
			var xp = j.data.path;
			if(gId==200)
			{
				xp = j.data.path.replace(".","sm."); 
			}
			$("#showImg").css("background-image","url(../"+xp+")");
			$("#showImg").css("background-position","center");
			$("#showImg").css("background-repeat","no-repeat");
			//$("#showImg").css("background-size","295px 235px");
			$("#author").html("作者："+j.data.author);
			$("#name").html("名称："+j.data.mark);
			$("#idid").html("编号："+j.data.id);
			$("#instro").html("出处说明："+j.data.instro);
			$("#uploader").html("上传："+j.data.uname);
			$("#zhuanTi").html("专题："+j.data.zhuanTi);
			$("#limitUse").html("使用限制："+j.data.limitUse);
			$("#avaliDate").html("使用时限："+j.data.avaliDate+"  到期");
			$("#showImg").find("#info").html(j.data.fileTypeName+" ["+j.data.size+"]");
		}
		else
		{
			window.location.href="index.html";
		}
		});
}
function down()
{
	window.location.href="../index.php?act=down&id="+gId;
}
function fav()
{
	$.get("../index.php?act=ajax&op=addFav&id="+gId,function(data){
	    if(data.indexOf("ok")!=-1)
		{
			alert("收藏成功");
		}
	});
}